package com.atguigu.minio;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinioDemo {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String bucketName = "b002" ;

        //1.创建MinioClient对象
        MinioClient client = MinioClient.builder()
                .endpoint("http://192.168.232.100:9000")
                .credentials("minioadmin","minioadmin")
                .build();

        //2.判断桶是否存在，不存在，则创建桶并设置访问策略
        boolean flag = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if(!flag){
            client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            String policyConfig = String.format("""
                    {
                              "Statement" : [ {
                                "Action" : "s3:GetObject",
                                "Effect" : "Allow",
                                "Principal" : "*",
                                "Resource" : "arn:aws:s3:::%s/*"
                              } ],
                              "Version" : "2012-10-17"
                            }
                    """,bucketName);
            client.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName)
                    .config(policyConfig)
                    .build());
        }

        //3.uploadObject上传
        client.uploadObject(UploadObjectArgs.builder()
                .bucket(bucketName)             //指定桶的名称
                .object("公寓-外观.jpg")
                .filename("D:\\atguigu2024\\workspace0328\\pro_lease\\pro02-minio\\src\\main\\resources\\imgs\\公寓-外观.jpg")
                .build());
    }
}
